<?php
namespace app\index\controller;

class Index extends Base
{
    public function index()
    {
        return $this->dislpay();
    }

    public function lists()
    {
        $cnav=999;
        open_debug();
        $id = input("id");
        $cate = model("cate");
        $article = model("article");
        $list = null;
        if ($id == "search") {
            $name=input("name");
            $list = $article->where("title", "like", "%$name%")->order("id desc")->paginate(10);
        } else {
            $cate = $cate->get($id);
            if ($cate == null) {
                return "known cate";
            }
            if ($cate['upid'] == "0") {
                $subcate = $cate->where("upid", $id)->select();
                if (count($subcate) != 0) {
                    $cidlist = [];
                    foreach ($subcate as $sub) {
                        $cidlist[] = $sub['id'];
                    }
                    $list = $article->where("cate_id", "in", implode(",", $cidlist))->order("id desc")->paginate($cate['count']);
                } else {
                    $list = $article->where("cate_id", $id)->order("id desc")->paginate($cate['count']);
                }
            }
            else{
                $list = $article->where("cate_id", $id)->order("id desc")->paginate($cate['count']);
            }
        }
        $i=1;
        foreach ($this->cms['navlist'] as $nav){
            if(strstr($nav['href'],"/list/$id")){
                $cnav=$i;
            }
            $i++;
        }
        $this->assign("cnav",$cnav);
        $this->assign("list",$list);
        return $this->dislpay("list");
    }

    public function article()
    {
        $cnav=999;
        open_debug();
        $id = input("id");
        $article = model("article");
        $article=$article->get($id);
        $i=1;
        foreach ($this->cms['navlist'] as $nav){
            if(strstr($nav['href'],"/article/$id")){
                $cnav=$i;
            }
            $i++;
        }
        $this->assign("cnav",$cnav);
        $this->assign("article",$article);
        return $this->dislpay();
    }

}
